import Vue from "vue";
import App from "./App.vue";
import router from "./routes/index";
import axios from "axios"; // 导入axios
import qs from "qs"; // 到诶qs，解决axios post不能传递参数
import store from "./store/store"; // 导入store
import iView from "iview"; // 导入iview
import "iview/dist/styles/iview.css"; // 导入iview样式
import serverIp from "../config/serverIp"; // 获取后台后台接口的IP和端口号
Vue.prototype.$qs = qs;
Vue.use(iView);

// 导入全局axios请求方法
axios.defaults.baseURL =
  serverIp.serverArgument + serverIp.serverHost + ":" + serverIp.serverPort;
axios.defaults.withCredentials = false; // 不携带cookie【携带cookie不支持跨域】
axios.interceptors.request.use(
  req => {
    console.log("req", req);
    const token = window.sessionStorage.getItem("token");
    req.headers.common["Authorization"] = String(token);
    return req;
  },
  err => {
    console.log("reqerr", err);
  }
);
axios.interceptors.response.use(
  res => {
    console.log("res", res);
    if (res.data.code !== 200) {
      window.sessionStorage.removeItem("token");
      router.replace({
        path: "/"
      });
    }
    return res;
  },
  err => {
    console.log("resErr", err);
    window.sessionStorage.removeItem("token");
    router.replace({
      path: "/"
    });
  }
);
Vue.prototype.$axios = axios; // 把axios扩展到Vue上

// 设置为 false 以阻止 vue 在启动时生成生产提示
Vue.config.productionTip = false;

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount("#app");
